// 发布订阅模式
const api = ['API:AUTH']
const websocket = ["WEBSOCKET:CONNECTION", "WEBSOCKET:SEND","WEBSOCKET:CHITCHAT"]
const router = ["ROUTER:SKIT"];
const eventNames = [...api,...websocket,...router];
class Subscribe{
  #listeners;
  constructor() {
    this.#listeners = new Map()
    for (let eventName of eventNames) {
      this.#listeners.set(eventName,new Set())
    }
  }


  on (eventName, fun) {//注册事件

     let events = this.#listeners.get(eventName)
    if (events === null) {
      events = new Set();
    }
    events.add(fun)
  }

  emits (eventName, ...args) {//触发事件
    const funs = [...this.#listeners.get(eventName).values()]
    let res = funs.map(fun => {
      return fun(...args);
    })
    return res;
  }

}

export default new Subscribe();